package com.example.demo.photoFile.controller;

import com.example.demo.photoFile.RadarChartLayers;
import com.example.demo.photoFile.service.FileService;
import com.example.demo.photoFile.util.RadarChartUtil;
import com.example.demo.util.ResultUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "文件")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation(httpMethod = "POST", value = "单张图片上传，大于5M会压缩")
    @PostMapping(path = "/uploadPhoto")
    public Map<String, Object> uploadPhoto(MultipartFile file, HttpServletRequest request) {
        return fileService.uploadPhoto(file, request);
    }

    @ApiOperation(httpMethod = "POST", value = "多张图片上传，大于5M会压缩")
    @PostMapping(path = "/uploadPhotos")
    public Map<String, Object> uploadPhotos(MultipartFile[] files, HttpServletRequest request) {
        return fileService.uploadPhotos(files, request);
    }

    @ApiOperation(httpMethod = "POST", value = "上传图片压缩包，解压保存至指定路径并保存数据库")
    @PostMapping(path = "/savePhotoInZip")
    public Map<String, Object> uploadPhotoFile(MultipartFile file, HttpServletRequest request) {
        String filetype = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (!filetype.equals(".zip")) {
            return ResultUtil.info(-1, "文件类型必须是zip格式");
        }
        return fileService.uploadPhotoFile(file, request);
    }

    @Value("${file-save-path}")
    private String fileSavePath;

    @ApiOperation(value = "下载图片，利用流，会占用内存，此方法适用单个文件")
    @GetMapping("downloadPhoto")
    public void downloadLeida() {
        List<String> code = new ArrayList<>();
        code.add("语文");
        code.add("数学");
        code.add("英语");
        List<RadarChartLayers> user = new ArrayList<>();
        user.add(new RadarChartLayers(2, "语文", 1L, 1L));
        user.add(new RadarChartLayers(3, "数学", 2L, 2L));
        user.add(new RadarChartLayers(1, "英语", 3L, 3L));

        RadarChartUtil.saveAsFile(fileSavePath, 3, code, user, 3L, 3L, 1);
    }
}
